<?php

/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/19 0019
 * Time: 下午 16:39
 */
class Order
{

    /**组合订单信息
     * @param $data 传递参数
     * @return mixed
     */
    static function OrganizationOrderData($data)
    {
        $order['add_time'] = time();
        $order['order_sn'] = get_order_sn();
        $order['pay_status'] = PAY_STATUS_0;
        $order['order_status'] = ORDER_STATUS_0;
        $order['store_id'] = $data['store'];
        $order['payment_id'] = $data['payment'];
        $order['shipping_id'] = $data['shipping'];
        $order['sum'] = $data['sum'];
        $order['total'] = $data['total'];
        $order['remark'] = $data['remark'];
        $order['consignee_name'] = $data['consignee_name'];
        $order['telephone'] = $data['consignee_telephone'];
        $order['consignee_address'] = $data['consignee_address'];
        $order['member_id'] = $_SESSION['userId'];

        foreach ($data['goodsArr'] as $k => $v) {
            $order['order_goods'][] = array(
                'goods_id' =>$k,
                'num' =>$v,
                'shop_price'=>M('goods')->where('id='.$k)->getField('shop_price')
            );
        }
        return $order;
    }

    /**提交或修改订单
     * @param $data 订单参数
     * @param int $orderId 修改订单id
     * @return bool|mixed
     */
    static function saveOrder($data, $orderId = 0,$isRX)
    {
        if (!$orderId) {
            $id=D('OrderRelation')->data($data)->add();
            if ($id) {
                return $id;
            } else {
                return false;
            }
        } else {
            $where=array(
                'id'=>$orderId
            );
            if ($isRX){
                $result=M('rx')->where($where)->save($data);
            }else{
                $result=D('OrderRelation')->where($where)->save($data);
            }
            if ($result) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**获取用户订单
     * @param $where 条件
     * @return mixed
     */
    static function getOrderArr($where){
        $db=M('order');
        $field=array('id','order_sn', 'payment_id', 'shipping_id', 'total', 'pay_time','add_time', 'pay_status', 'order_status','consignee_address','consignee_name','telephone');
        $orderArr=$db->order('id DESC')->field($field)->where($where)->select();
        return $orderArr;
    }

    /**获取订单详细信息
     * @param $orderId 订单id
     * @param int $memberId 会员id
     * @return mixed
     */
    static function getOrderInfo($orderId,$memberId=0){
        $db=D('OrderRelation');
        if($memberId){
            $where=array(
                'id'=>$orderId,
                'member_id'=>$memberId
            );
        }else{
            $where=array(
                'id'=>$orderId
            );
        }
        
        $orderInfo=$db->relation(true)->where($where)->find();
        return $orderInfo;
    }

    /**按订单状态匹配状态中文
     * @param $status 订单状态
     * @return mixed|string
     */
    static function getOrderStatusName($status){
        $name=constant('ORDER_STATUS_'.$status.'_NAME');
        if(empty($name)){
            $name='未确认';
        }
        return $name;
    }

    /**改变订单状态
     * @param $status 状态值
     * @param $orderId 订单id
     * @param int $memberId 会员id
     * @return bool|mixed
     */
    static function changeOrderStatus($status,$orderId,$memberId=0){
        if($memberId){
            $where=array(
                'id'=>$orderId,
                'member_id'=>$memberId
            );
        }else{
            $where=array(
                'id'=>$orderId,
            );
        }

        if(M('order')->where($where)->save(array('order_status'=>$status))){
            return true;
        }else{
            return false;
        }
    }

    /**编辑处方登记状态
     * @param $RxId处方登记id
     * @param $pass 审核是否通过
     * @return bool
     */
    static function editRxStatus($RxId,$pass){
        if($pass){
            $data=array(
                'order_status'=>ORDER_STATUS_1,
                'rx_status'=>RX_STATUS_1
            );
        }else{
            $data=array(
                'order_status'=>ORDER_STATUS_1,
                'rx_status'=>RX_STATUS_2
            );
        }

        if(M('rx')->where(array('id'=>$RxId))->save($data)){
            return true;
        }else{
            return false;
        }
    }
    
}
